package com.huadi.qi.controller;

import com.huadi.qi.bean.User;
import com.huadi.qi.mapper.UserMapper;
import com.huadi.qi.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOError;
import java.io.IOException;
import java.util.List;
import java.util.Random;

@Controller
public class UserController {
    @Autowired
    UserMapper userMapper;
    @Autowired
    UserService userService;
    //设置
    @GetMapping("/setting")
    public String set(){
        return "setting";
    }

    //在线答疑
    @GetMapping("/chat")
    public String chat(){
        return "chat";
    }

    //基本资料
    @GetMapping("/material")
    public String material(){
        return "material";
    }

    //添加用户信息
    @GetMapping("/add")
    public String add(){
        return "/admin/add";
    }
    @PostMapping("/add")
    public String postAdd(Integer age, String username, String password, MultipartFile avatar,
                          String email, String sex, String tel, String grade, HttpSession session)throws IOException {
        //图片服务器

        if (!avatar.isEmpty()){
            String originalFilename = avatar.getOriginalFilename();

//            String h = originalFilename.substring(originalFilename.length()-4);
        }
            Random random = new Random();
            int q = random.nextInt(80000);
            String h = ".jpg";
            String filename = q+h;
            avatar.transferTo(new File("F:\\华迪培训资料\\项目\\qi\\src\\main\\resources\\static\\img\\TouXiang\\"+filename));
        try {
            User user = new User();
            user.setAge(age);
            user.setEmail(email);
            user.setGrade(grade);
            user.setPassword(password);
            user.setSex(sex);
            user.setTel(tel);
            user.setUsername(username);
            user.setAvatar(filename);
            userMapper.insert(user);
            session.setAttribute("userMsg","添加成功");
            return "redirect:/admin/user";
        }catch (Exception e){
            session.setAttribute("userMsg","添加失败");
            return "redirect:/admin/user";
        }
    }
    //删除用户信息
    @GetMapping("/delete{id}")
    public String delete(@PathVariable("id") String id, @RequestParam(value = "pn", defaultValue = "1") Integer pn,
                         RedirectAttributes re, HttpSession session){
        try {
            boolean b = userService.removeById(id);
            if (b == true) {
                session.setAttribute("userMsg","删除成功");
                re.addAttribute("pn", pn);
                return "redirect:/admin/user";
            }
            session.setAttribute("userMsg","删除失败");
            re.addAttribute("pn", pn);
            return "redirect:/admin/user";
        }catch (Exception e){
            e.printStackTrace();
            re.addAttribute("pn", pn);
            session.setAttribute("userMsg","删除失败");
            return "redirect:/admin/user";
        }
    }

    //修改用户信息
    @GetMapping("/update{id}")
    public String update(@PathVariable("id") Integer id, Model model){
        User byId = userService.getById(id);
        model.addAttribute("user",byId);
        return "/admin/update";
    }

    @PostMapping("/post/userUpdate")
    public String postUpdate(Integer id,Integer age, String username, String password, MultipartFile avatar,
                          String email, String sex, String tel, String grade, HttpSession session)throws IOException {
        //图片服务器

        if (!avatar.isEmpty()){
            String originalFilename = avatar.getOriginalFilename();

//            String h = originalFilename.substring(originalFilename.length()-4);
        }
        Random random = new Random();
        int q = random.nextInt(80000);
        String h = ".jpg";
        String filename = q+h;
        avatar.transferTo(new File("F:\\华迪培训资料\\项目\\qi\\src\\main\\resources\\static\\img\\TouXiang\\"+filename));
        try {
            User user = new User();
            user.setId(id);
            user.setAge(age);
            user.setEmail(email);
            user.setGrade(grade);
            user.setPassword(password);
            user.setSex(sex);
            user.setTel(tel);
            user.setUsername(username);
            user.setAvatar(filename);
            boolean b = userService.updateById(user);
            if (b == true){
                session.setAttribute("userMsg","修改成功");
                return "redirect:/admin/user";
            }
//            userMapper.updateById(user);
            session.setAttribute("userMsg","修改失败");
            return "redirect:/admin/user";
        }catch (Exception e){
            session.setAttribute("userMsg","修改失败");
            return "redirect:/admin/user";
        }
    }


}
